package constant.milk.toilet.dialog;

import java.util.Calendar;

import constant.milk.toilet.R;
import constant.milk.toilet.util.MilkUtil;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MilkDateDialog extends Dialog
{
	private Context context;
	private float TEXT_SIZE;
	
	private View contentView;
	private TextView titleTextView;
	
	private TextView yearPlusTextView;
	private TextView yearTextView;
	private TextView yearMinuseTextView;
	
	private TextView monthPlusTextView;
	private TextView monthTextView;
	private TextView monthMinuseTextView;
	
	private TextView dayPlusTextView;
	private TextView dayTextView;
	private TextView dayMinuseTextView;
	
	private TextView settingButton;
	private TextView cencelButton;
	
	private int year;
	private int month;
	private int day;
	
	private DateCollBank collbank = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.dialog_date);
		
		setSettingFontSize();
		
		setFindView();
		setOnClick();
		setData();
	}

	private void setFindView()
	{
		contentView = findViewById(R.id.dialogDateContentView);
		titleTextView = (TextView) findViewById(R.id.dialogDateTitleTextView);
		
		yearPlusTextView = (TextView) findViewById(R.id.dialogDateYearPlusTextView);
		yearTextView = (TextView) findViewById(R.id.dialogDateYearTextView);
		yearMinuseTextView = (TextView) findViewById(R.id.dialogDateYearMinuseTextView);
		
		monthPlusTextView = (TextView) findViewById(R.id.dialogDateMonthPlusTextView);
		monthTextView = (TextView) findViewById(R.id.dialogDateMonthTextView);
		monthMinuseTextView = (TextView) findViewById(R.id.dialogDateMonthMinuseTextView);
		
		dayPlusTextView = (TextView) findViewById(R.id.dialogDateDayPlusTextView);
		dayTextView = (TextView) findViewById(R.id.dialogDateDayTextView);
		dayMinuseTextView = (TextView) findViewById(R.id.dialogDateDayMinuseTextView);
		
		settingButton = (TextView) findViewById(R.id.dialogDateSettingTextView);
		cencelButton = (TextView) findViewById(R.id.dialogDateCencelTextView);
	}
	
	private void setOnClick()
	{
		yearPlusTextView.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Calendar cal = Calendar.getInstance();
				cal.set(Calendar.YEAR, year);
				cal.add(Calendar.YEAR, 1);
				year = cal.get(Calendar.YEAR);
				
				yearTextView.setText(MilkUtil.getDateStr(year));
				titleTextView.setText(MilkUtil.getDateCom(year, month+1, day));
			}
		});
		
		yearMinuseTextView.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Calendar cal = Calendar.getInstance();
				cal.set(Calendar.YEAR, year);
				cal.add(Calendar.YEAR, -1);
				year = cal.get(Calendar.YEAR);
				
				yearTextView.setText(MilkUtil.getDateStr(year));
				titleTextView.setText(MilkUtil.getDateCom(year, month+1, day));
			}
		});
		
		monthPlusTextView.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Calendar cal = Calendar.getInstance();
				cal.set(Calendar.MONTH, month);
				cal.add(Calendar.MONTH, 1);
				month = cal.get(Calendar.MONTH);
				
				monthTextView.setText(MilkUtil.getDateStr(month+1));
				titleTextView.setText(MilkUtil.getDateCom(year, month+1, day));
			}
		});
		
		monthMinuseTextView.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Calendar cal = Calendar.getInstance();
				cal.set(Calendar.MONTH, month);
				cal.add(Calendar.MONTH, -1);
				month = cal.get(Calendar.MONTH);
				
				monthTextView.setText(MilkUtil.getDateStr(month+1));
				titleTextView.setText(MilkUtil.getDateCom(year, month+1, day));
			}
		});
		
		dayPlusTextView.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Calendar cal = Calendar.getInstance();
				cal.set(Calendar.DAY_OF_MONTH, day);
				cal.add(Calendar.DAY_OF_MONTH, 1);
				day = cal.get(Calendar.DAY_OF_MONTH);
				
				dayTextView.setText(MilkUtil.getDateStr(day));
				titleTextView.setText(MilkUtil.getDateCom(year, month+1, day));
			}
		});
		
		dayMinuseTextView.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Calendar cal = Calendar.getInstance();
				cal.set(Calendar.DAY_OF_MONTH, day);
				cal.add(Calendar.DAY_OF_MONTH, -1);
				day = cal.get(Calendar.DAY_OF_MONTH);
				
				dayTextView.setText(MilkUtil.getDateStr(day));
				titleTextView.setText(MilkUtil.getDateCom(year, month+1, day));
			}
		});
		
		settingButton.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				collbank.getDate(year, month, day);
				dismiss();
			}
		});
		
		cencelButton.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				dismiss();
			}
		});
	}
	
	private void setData()
	{
		titleTextView.setTextSize(TEXT_SIZE);
		
		yearPlusTextView.setTextSize(TEXT_SIZE);
		yearTextView.setTextSize(TEXT_SIZE);
		yearMinuseTextView.setTextSize(TEXT_SIZE);
		
		monthPlusTextView.setTextSize(TEXT_SIZE);
		monthTextView.setTextSize(TEXT_SIZE);
		monthMinuseTextView.setTextSize(TEXT_SIZE);
		
		dayPlusTextView.setTextSize(TEXT_SIZE);
		dayTextView.setTextSize(TEXT_SIZE);
		dayMinuseTextView.setTextSize(TEXT_SIZE);
		
		settingButton.setTextSize(TEXT_SIZE);
		cencelButton.setTextSize(TEXT_SIZE);
		
		titleTextView.setTextColor(Color.WHITE);
		settingButton.setTextColor(Color.WHITE);
		cencelButton.setTextColor(Color.WHITE);
		yearPlusTextView.setTextColor(Color.BLACK);
		yearTextView.setTextColor(Color.BLACK);
		yearMinuseTextView.setTextColor(Color.BLACK);
		monthPlusTextView.setTextColor(Color.BLACK);
		monthTextView.setTextColor(Color.BLACK);
		monthMinuseTextView.setTextColor(Color.BLACK);
		dayPlusTextView.setTextColor(Color.BLACK);
		dayTextView.setTextColor(Color.BLACK);
		dayMinuseTextView.setTextColor(Color.BLACK);
		
		titleTextView.setText(MilkUtil.getDateCom(year, month+1, day));
		
		yearTextView.setText(MilkUtil.getDateStr(year));
		monthTextView.setText(MilkUtil.getDateStr(month+1));
		dayTextView.setText(MilkUtil.getDateStr(day));
	}

	public MilkDateDialog(Context context, DateCollBank collbank, int year, int month, int day)
	{
		super(context, android.R.style.Theme_Translucent_NoTitleBar);
		
		this.context = context;
		this.collbank = collbank;
		this.year = year;
		this.month = month;
		this.day = day;
	}
	
	private void setSettingFontSize()
	{
		int fontSize = 9;
		
		SharedPreferences p = context.getSharedPreferences("pref", Activity.MODE_PRIVATE);
		int fontFlag = p.getInt(context.getString(R.string.prefFont), 1);
		
		if (fontFlag == 0) fontSize = 13;
		else if (fontFlag == 1) fontSize = 16;
		else if (fontFlag == 2) fontSize = 20;
		
		TEXT_SIZE = fontSize;
	}
	
	public interface DateCollBank
	{
		public abstract void getDate(int year, int month, int day);
	}
}
